#include "stdafx.h"
#include "Plane.h"

namespace PhotonMapping
{

Plane::Plane(const Math::Vector3& p_Normal, const float p_Dist, const intx p_Material)
	: IObject(p_Material), m_Normal(p_Normal), m_Dist(p_Dist)
{
	m_Normal.Normalize();

	// calc bounding sphere
	//m_BoundingSphere.m_Center = p_P0 + halfDiag;
	//m_BoundingSphere.m_Radius = halfDiag.Length();
}


intx Plane::TestIntersection(const Math::Ray& p_Ray, float &p_Distance)
{
	using namespace Math;

	// check if ray is parallel to plane
	const float denominator = m_Normal * p_Ray.m_Direction;
	if(denominator < 0.0f)	
	{
		float t = -(m_Normal * p_Ray.m_Position + m_Dist) / denominator;
		if(t > 0.0f && t < p_Distance)
		{
			// intersecting
			p_Distance = t;
			return HIT;
		}
	}
	
	return IObject::MISS;
}


const Math::Vector3 Plane::GetNormalAt(const Math::Vector3&)
{
	return m_Normal;
}

const Math::Sphere& Plane::GetBoundingSphere() const
{
	return m_BoundingSphere;
}

}
